﻿using Microsoft.Phone.Shell;
using WP8_PPS.Helpers;
using WP8_PPS.Models;

namespace WP8_PPS.ViewModels
{
    /// <summary>
    /// View Model for the the Single Survey Entry Page which will prepare the data to View
    /// </summary>
    public class SingleSurveyEntryViewModel : BindableBase
    {
        #region Fields
        private double _longitude;
        private double _latitude;
        private PoleCondition _condition;
        private ushort _year;
        private float _height;
        private string _comments;
        #endregion

        #region Constructor
        /// <summary>
        /// Constructor which hooks up commands and resets data
        /// </summary>
        public SingleSurveyEntryViewModel()
        {
            this.LocateCommand = new RelayCommand(ExecuteLocateCommand);
            this.SaveCommand = new RelayCommand(ExecuteSaveCommand);

            this.Reset();
        }
        #endregion

        #region Properties
        public double Longitude
        {
            get { return this._longitude; }
            set { this.SetProperty(ref this._longitude, value); }
        }
        public double Latitude
        {
            get { return this._latitude; }
            set { this.SetProperty(ref this._latitude, value); }
        }
        public PoleCondition Condition
        {
            get { return this._condition; }
            set { this.SetProperty(ref this._condition, value); }
        }
        public ushort Year
        {
            get { return this._year; }
            set { this.SetProperty(ref this._year, value); }
        }
        public float Height
        {
            get { return this._height; }
            set { this.SetProperty(ref this._height, value); }
        }
        public string Comments
        {
            get { return this._comments; }
            set { this.SetProperty(ref this._comments, value); }
        }
        public RelayCommand LocateCommand
        {
            get;
            private set;
        }
        public RelayCommand SaveCommand
        {
            get;
            private set;
        }
        #endregion

        #region Command Execute
        /// <summary>
        /// Command to Locate using GPS Service
        /// </summary>
        private async void ExecuteLocateCommand()
        {
            this.ProgressBar(true, "Aquiring GPS Signal..." );

            var location = await Services.ServiceLocator.GeolocatorService.GetOneShotLocationAsync();
            this.Longitude = location.Longitude;
            this.Latitude = location.Latitude;

            this.ProgressBar(false);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="isVisible"></param>
        /// <param name="text"></param>
        private void ProgressBar(bool isVisible, string text = "")
        {
            SystemTray.ProgressIndicator.Text = text;
            SystemTray.ProgressIndicator.IsIndeterminate = isVisible;
            SystemTray.ProgressIndicator.IsVisible = isVisible;
        }
        /// <summary>
        /// Command to Save data to Collection
        /// </summary>
        private void ExecuteSaveCommand()
        {
            PowerPoleSurveyCollection.AddToCollection(new PowerPoleSurvey()
                {
                    Longitude = this.Longitude,
                    Latitude = this.Latitude,
                    Comments = this.Comments,
                    Year = this.Year,
                    Condition = this.Condition,
                    Height = this.Height
                });

            this.Reset();
        }

        private void Reset()
        {
            this.Longitude = 0;
            this.Latitude = 0;
            this.Height = 0;
            this.Year = 0;
            this.Condition = PoleCondition.Excellent;
            this.Comments = "";
        }
        #endregion
    }
}
